<html>
<head>
	<title>Survey</title>
	<meta charset="utf-8" />
	<meta name="viewport" content="width=device-width; initial-scale=1.0; maximum-scale=1.0;" />
	<meta name="format-detection" content="telephone=no" />
	
	<link rel="stylesheet" href="css/style.css" />
	<link rel="stylesheet" href="css/search.css" />
	
	<script type="text/javascript" src="js/auto_complete/jquery-1.2.1.pack.js"></script>
	
	<script type="text/javascript" src="js/auto_complete/suggest.js"></script>
	
	<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=true&libraries=geometry"></script>
	
</head>
<body>
<form action="" method="post">
<table cellpadding="0">
<tr>
	<td valign="top">Company ID:</td><td>
							<!--<input type="text" name="id" onkeyup="id_hint(this.value)"/>-->
							<input type="text" name="id" />
							<div id="id_suggest"></div>
						</td>
</tr>
<tr>
	<!--<td></td><td><div id="id_suggest"></div></td>-->
</tr>
<tr>
	<td valign="top">Company Name:</td><td><input type="text" id="name" name="name" onkeyup="name_hint(this.value)" onblur="name_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="name_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="name_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
						
</tr>
<tr>
	<td valign="top">Sector:</td><td><input type="text" id="sector" name="sector" onkeyup="sector_hint(this.value)" onblur="sector_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="sector_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="sector_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
</tr>
<tr>
	<td valign="top">Subsector</td><td><input type="text" id="subsector" name="subsector" onkeyup="subsector_hint(this.value)" onblur="subsector_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="subsector_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="subsector_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
</tr>
<tr>
	<td valign="top">Company Type:</td><td><input type="text" id="type" name="type" onkeyup="type_hint(this.value)" onblur="type_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="type_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="type_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
</tr>
<tr>
	<td valign="top">Street Address:</td><td><input type="text" id="address" name="address" onkeyup="addr_hint(this.value)" onblur="addr_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="addr_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="addr_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
</tr>
<tr>
	<td valign="top">City:</td><td><input type="text" id="city" name="city" onkeyup="city_hint(this.value)" onblur="city_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="city_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="city_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
</tr>
<tr>
<td>State:</td><td>
<?php include 'state.php';
	echo '<select name="state">';
	// first value is blank
	echo "<option value=''></option>";
	foreach($states as $k=>$s) {
		echo '<option value="'.$k.'">'.$s.'</option>';
	}
	echo '</select>';
?></td></tr>
<tr>
<td valign="top">Zipcode:</td><td><input type="text" id="zipcode" name="zipcode" onkeyup="zipcode_hint(this.value)" onblur="zipcode_fill();" autocomplete="off"/>
								<div class="suggestionsBox" id="zipcode_suggest" style="display: none;">
									<img src="images/upArrow.png" style="position: relative; top: -12px; left: 30px;" alt="upArrow" />
									<div class="suggestionList" id="zipcode_autoSuggestionsList">
										&nbsp;
									</div>
								</div>
							</td>
</tr>
<tr>
<td>Phone:</td><td><input type="text" name="phone" /></td>
</tr>
<tr>
<td>M_site exists</td>
<td>
	<select name="m_site_exists">
		<option value=""></option>
		<option value="y">Yes</option>
		<option value="n">No</option>
	</select>
</td>
</tr>
<tr>
<td>Note:</td><td><input type="text" name="note" /></td>
</tr>
</table>
<input type="submit" name="submit" value="Search" />
</form>
<?php 
include 'config.php';
if (isset($_POST['submit'])) {
	$conn = mysql_connect($host, $user, $password) or die(mysql_error());
	mysql_select_db("mweb_survey");
	
	$id = $_POST['id'];
	$name = $_POST['name'];
	$type = $_POST['type'];
	$sector = $_POST['sector'];
	$subsector = $_POST['subsector'];
	$address = $_POST['address'];
	$city = $_POST['city'];
	$state = $_POST['state'];
	$zipcode = $_POST['zipcode'];
	$phone = $_POST['phone'];
	$ms = $_POST['m_site_exists'];
	// $note = $_POST['note'];
	
	$sql = "SELECT * FROM company";
	if ($id=='' && $name=='' && $type=='' && $sector=='' && $subsector=='' && $address=='' && $city=='' && $state=='' && $zipcode=='' && $phone=='' && $ms=='') {
		echo "Error/Warning: You should fill at least one field.";
	} else {
		$flag = 0;
		$sql .= " WHERE ";
		
		if ($id!='') { 
			$sql .= " CompID like '%$id%' ";
			$flag = 1;
		}
		if ($name!='') {
			if ($flag == 1) {
				$sql .= " AND CompName like '%$name%' ";
			} else {
				$sql .= " CompName like '%$name%' ";
				$flag = 1;
			}
		}
		if ($type!='') {
			if ($flag == 1) {
				$sql .= " AND Type like '%$type%' ";
			} else {
				$sql .= " Type like '%$type%' ";
				$flag = 1;
			}
		}
		if ($sector!='') {
			if ($flag == 1) {
				$sql .= " AND Sector like '%$sector%' ";
			} else {
				$sql .= " Sector like '%$sector%' ";
				$flag = 1;
			}
		}
		if ($subsector!='') {
			if ($flag == 1) {
				$sql .= " AND Subsector like '%$subsector%' ";
			} else {
				$sql .= " Subsector like '%$subsector%' ";
				$flag = 1;
			}
		}
		if ($address!='') {
			if ($flag == 1) {
				$sql .= " AND StreetAddr like '%$address%' ";
			} else {
				$sql .= " StreetAddr like '%$address%' ";
				$flag = 1;
			}
		}
		if ($city!='') {
			if ($flag == 1) {
				$sql .= " AND City like '%$city%' ";
			} else {
				$sql .= " City like '%$city%' ";
				$flag = 1;
			}
		}
		if ($state!='') {
			if ($flag == 1) {
				$sql .= " AND State='$state' ";
			} else {
				$sql .= " State='$state' ";
				$flag = 1;
			}
		}
		if ($zipcode!='') {
			if ($flag == 1) {
				$sql .= " AND Zip like '%$zipcode%' ";
			} else {
				$sql .= " Zip like '%$zipcode%' ";
				$flag = 1;
			}
		}
		if ($phone!='') {
			if ($flag == 1) {
				$sql .= " AND Phone like '%$phone%' ";
			} else {
				$sql .= " Phone like '%$phone%' ";
				$flag = 1;
			}
		}
		if ($ms!='') {
			if ($flag == 1) {
				$sql .= " AND m_website = '$ms' ";
			} else {
				$sql .= " m_website = '$ms' ";
				$flag = 1;
			}
		}
		
		$result_comp = mysql_query($sql, $conn) or die(mysql_error());
		$result_count = mysql_num_rows($result_comp);
		if (mysql_num_rows($result_comp)==0) {
			echo "No records matched criteria.";
		} else {
			echo "<b>Search Results ($result_count)</b><hr>";
			//
			// function timeAndMilliseconds()
			// {
				// $m = explode(' ',microtime());
				// return array($m[1], (int)round($m[0]*1000,3));
			// } 
			
			// list($totalSeconds, $extraMilliseconds) = timeAndMilliseconds();
			// $starttime = date("d-m-Y H:i:s", $totalSeconds) . ".$extraMilliseconds\n";
			
			//
			echo "<form action='route.php' method='post'>";
			while ($row_comp = mysql_fetch_assoc($result_comp)) {
				$comp_id = $row_comp['CompID'];
				$comp_name = $row_comp['CompName'];
				$comp_type = $row_comp['Type'];
				$comp_sector = $row_comp['Sector'];
				$comp_sub_sector = $row_comp['Subsector'];
				$comp_addr = $row_comp['StreetAddr'] . ", " . $row_comp['City'] . ", " . $row_comp['State'] . ", " . $row_comp['Zip'];
				$comp_phone = $row_comp['Phone'];
				$comp_url = $row_comp['PC_website'];
				$comp_note = $row_comp['Notes'];
				echo "<input type='checkbox' name='check_comp[]' value='$comp_id' />Company Name: $comp_name<br/>";
				echo "Sector: $comp_sector<br/>";
				echo "Subsector: $comp_sub_sector<br/>";
				echo "Company Type: $comp_type<br/>";
				echo "Address: <a href='map.php?address=$comp_addr&compName=$comp_name'>$comp_addr</a><br/>";
				echo "Phone: <a href='tel:$comp_phone'>$comp_phone</a><br/>";
				echo "URL: <a href='http://$comp_url'>$comp_url</a><br/>";
				
				$sql_check = "SELECT * FROM answers WHERE CompID='$comp_id'";
				$result_check = mysql_query($sql_check, $conn) or die(mysql_error());
				if (mysql_num_rows($result_check)==0) {
					echo "Notes: $comp_note<br/>";
					echo "<input type='button' onclick=\"window.location.href='question.php?comp_id=$comp_id'\" value='Do Survey' />";
				} else {
					$row = mysql_fetch_assoc($result_check);
					$datetime = $row['datetime'];
					echo "Survey Date: $datetime<br/>";
					echo "Notes: $comp_note<br/>";
					echo "<input type='button' onclick=\"window.location.href='showsurvey.php?comp_id=$comp_id'\" value='Show Survey' />";
				}
				
				echo "<br/><br/><a href='#top'>Top</a><a style='float:right' href='#bottom'>Bottom</a>";
				echo "<hr>";
			}
			//
			
			// list($totalSeconds, $extraMilliseconds) = timeAndMilliseconds();
			// $endtime = date("d-m-Y H:i:s", $totalSeconds) . ".$extraMilliseconds\n";
			
			// echo $starttime . "???????????" . $endtime;
			
			//
		}
		
		echo "$result_count results found.";
		echo "<hr>";
		
		echo "Start: <input type='text' name='start' /><br/>";
		echo "<input type='submit' value='Route Map' />";
		echo "</form>";
	}
}
?>
<?php include 'footer.php';?>
</body>
</html>
